//
// Created by dxyinme on 2021/10/23.
//

#include "util.h"

namespace ux_png {

bool IsPNG(const char * filename) {
  auto fp = fopen(filename, "rb");
  if (fp == nullptr) {
    return false;
  }
  auto header_ptr = new char[PNG_CHECK_LENGTH];
  bool ret;
  if (fread(header_ptr, 1, PNG_CHECK_LENGTH, fp) != PNG_CHECK_LENGTH) {
    ret = false;
  } else {
    ret = png_sig_cmp(reinterpret_cast<png_const_bytep>(header_ptr), 0, PNG_CHECK_LENGTH);
  }
  delete[] header_ptr;
  fclose(fp);
  return ret == 0;
}

int ColorType2Channels(int color_type) {
  switch (color_type) {
    case PNG_COLOR_TYPE_RGBA: return 4;
    case PNG_COLOR_TYPE_RGB:  return 3;
    case PNG_COLOR_TYPE_GRAY: return 1;
    case PNG_COLOR_TYPE_GA:   return 2;
    default: return 0;
  }
}

std::string ColorType2String(int color_type) {
  switch (color_type) {
    case PNG_COLOR_TYPE_RGBA: return "rgb-alpha";
    case PNG_COLOR_TYPE_RGB:  return "rgb";
    case PNG_COLOR_TYPE_GRAY: return "gray";
    case PNG_COLOR_TYPE_GA:   return "gray-alpha";
    default: return "";
  }
}


}